package org.iori.pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSmartCopy;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfMerge {

	/**
	 * @param args
	 * @throws DocumentException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws DocumentException, IOException {
		// TODO Auto-generated method stub
		String[] files = { "/Users/xing_lun/Documents/tifftest/mt0012-121130.pdf", "/Users/xing_lun/Documents/tifftest/mt0012-121130.pdf"};
		String savepath = "";
		
		savepath = "/Users/xing_lun/Documents/tiffoutput/mergePdfWriter.pdf";
		mergePdfWriter(files, savepath);
		
		savepath = "/Users/xing_lun/Documents/tiffoutput/mergePdfSmartCopy.pdf";
		mergePdfSmartCopy(files, savepath);
		
		savepath = "/Users/xing_lun/Documents/tiffoutput/mergePdfFiles.pdf";
		mergePdfFiles(files, savepath);
	}
	
	public static boolean mergePdfWriter(String[] files, String newfile) {
		boolean retValue = false;
		Document document = null;
		try {
			PdfReader reader = new PdfReader(files[0]);
			Rectangle psize = reader.getPageSize(1);
			float width = psize.getHeight();
			float height = psize.getWidth();
			document = new Document(new Rectangle(width, height * files.length + 20));
			
//			document = new Document(new PdfReader(files[0]).getPageSize(1));
			
			FileOutputStream fout = new FileOutputStream(new File(newfile));
			PdfWriter writer = PdfWriter.getInstance(document, fout);
			
			document.open();
			for (int i = 0; i < files.length; i++) {
				reader = new PdfReader(files[i]);
				int pageNum = reader.getNumberOfPages();
				
				for(int p = 1; p <= pageNum; p++){
					PdfImportedPage page = writer.getImportedPage(reader, p);
					PdfContentByte cb = writer.getDirectContent();
//					cb.addTemplate(page, 1f, 0, 0, 1f, page.getWidth() * i, page.getHeight() * (i + 1));
					cb.addTemplate(page, 0, height * i);
				}
			}   
			retValue = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			document.close();
		}
		return retValue;
	}
	
	public static boolean mergePdfSmartCopy(String[] files, String newfile) {
		boolean retValue = false;
		Document document = null;
		try {
			document = new Document(new PdfReader(files[0]).getPageSize(1));
			
			FileOutputStream fout = new FileOutputStream(new File(newfile));
			PdfSmartCopy copy= new PdfSmartCopy(document, fout);
			
			document.open();
			for (int i = 0; i < files.length; i++) {
				PdfReader reader = new PdfReader(files[i]);
				int pageNum = reader.getNumberOfPages();
				for(int p = 1; p <= pageNum; p++){
					PdfImportedPage page = copy.getImportedPage(reader, p);
					copy.addPage(page);
				}
			}   
			retValue = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			document.close();
		}
		return retValue;
	}
	
	  public static void mergePdfFiles(String[] files, String savepath)  
	    {  
	        try   
	        {  
	            Document document = new Document(new PdfReader(files[0]).getPageSize(1));  
	              
	            PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));  
	              
	            document.open();  
	              
	            for(int i=0; i<files.length; i++)  
	            {  
	                PdfReader reader = new PdfReader(files[i]);  
	                  
	                int n = reader.getNumberOfPages();  
	  
	                for(int j=1; j<=n; j++)  
	                {  
	                    document.newPage();   
	                    PdfImportedPage page = copy.getImportedPage(reader, j);
	                    copy.addPage(page);
//	                    copy.addToBody(page.getPdfDocument());
	                }  
	            }  
	              
	            document.close();  
	  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } catch(DocumentException e) {  
	            e.printStackTrace();  
	        }  
	    }  

}
